package br.ufrj.neg.reuniao.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import br.ufrj.neg.bean.TraducaoBean;
import br.ufrj.neg.model.ServiceLocator;
import br.ufrj.neg.model.TraducaoModel;
import br.ufrj.neg.reuniao.bean.TipoArtefatoBean;

public class TipoArtefatoModel {
	
	public Collection<TipoArtefatoBean> listaTipoArtefato(TipoArtefatoBean taB) {
			ResultSet rs = null;
			PreparedStatement ps=null;;
			Connection conn = ServiceLocator.getConnection();
			try {				
				Collection<TipoArtefatoBean> col = new ArrayList<TipoArtefatoBean>();
				TipoArtefatoBean tipoArtefato;
				String sql = "select * from tipo_artefato order by tiar_tx_descricao asc";
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()){
					tipoArtefato = taB.getNewInstance();
					tipoArtefato.setId(rs.getInt("tiar_cd_id"));
					//tipoArtefato.setDescricao(rs.getString("tiar_tx_descricao"));					
					TraducaoBean traducao = TraducaoModel.getTraducoesElemento(tipoArtefato);			
					tipoArtefato.setDicionario( traducao.getDicionario() );	
										
					col.add(tipoArtefato);
				}
				return col;
			} catch (Exception e) {
				System.out.println(e);
				return null;
			}finally{
				try{rs.close();}catch(Exception e){System.out.println(e);}
				try{ps.close();}catch(Exception e){System.out.println(e);}
				try{conn.close();}catch(Exception e){System.out.println(e);}
			}
		}
	
	public TipoArtefatoBean getTipoArtefato(TipoArtefatoBean tab){
		ResultSet rs = null;
		PreparedStatement ps=null;;
		Connection conn = ServiceLocator.getConnection();
		try {
			TipoArtefatoBean tipoArtefato = null;
			String sql = "select * from tipo_artefato where tiar_cd_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, tab.getId());
			rs = ps.executeQuery();
			while(rs.next()){
				tipoArtefato = tab.getNewInstance();
				tipoArtefato.setId(rs.getInt("tiar_cd_id"));
				//tipoArtefato.setDescricao(rs.getString("tiar_tx_descricao"));
				
				TraducaoBean traducao = TraducaoModel.getTraducoesElemento(tipoArtefato);			
				tipoArtefato.setDicionario( traducao.getDicionario() );					
			}
			return tipoArtefato;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		}finally{
			try{rs.close();}catch(Exception e){System.out.println(e);}
			try{ps.close();}catch(Exception e){System.out.println(e);}
			try{conn.close();}catch(Exception e){System.out.println(e);}
		}	
	
	}
}
